package org.fhm.zdte.common.constant.protocol;

import org.fhm.zdte.common.exception.ProtocolException;

import java.util.Objects;

/**
 * Content type.
 *
 * @author 谭波
 * @since 2024/3/29
 */
public enum ContentType {

    TEXT((byte) 0b00000000, "TEXT"),
    FILE((byte) 0b00000001, "FILE"),
    ;

    private final Byte valueBit;

    private final String name;

    ContentType(Byte valueBit, String name) {
        this.valueBit = valueBit;
        this.name = name;
    }

    public static ContentType getContentType(Byte valueBit) {
        if (Objects.isNull(valueBit))
            throw new ProtocolException("the Byte of content type error");
        for (ContentType contentType : ContentType.values()) {
            if (contentType.valueBit.equals(valueBit))
                return contentType;
        }
        throw new ProtocolException("there is no matching content type for Byte");
    }

    public static ContentType getContentType(String type) {
        if (Objects.isNull(type) || type.isEmpty())
            throw new ProtocolException("the Byte of content type error");
        for (ContentType contentType : ContentType.values()) {
            if (contentType.name.equals(type))
                return contentType;
        }
        throw new ProtocolException("there is no matching content type for Byte");
    }

    public Byte getValueBit() {
        return valueBit;
    }

    public String getName() {
        return name;
    }
}
